package com.xkdx.guangguang.sql;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.xkdx.guangguang.shareclass.BrandDetail;

public class BrandDataHelper {
	public static String TAG = "DataHelper";
	private String TABNAME = "brand";
	private static int DB_VERSION = 2;
	private SQLiteDatabase db;

	public BrandDataHelper(Context context, int table, boolean cityVersion) {

		ReleaseDataBase releaseDataBase = new ReleaseDataBase(context);
		db = releaseDataBase.OpenDataBase();

	}

	public void delet() {
		db.delete(TABNAME, null, null);
	}

	public void Close() {
		if (db != null) {
			db.close();
		}
	}

	public void inSert(List<BrandDetail> list) {
		if (list == null || db == null) {
			return;
		}
		try {
			Cursor c = db.query(TABNAME, null, null, null, null, null, null);
			if (!c.moveToNext()) {
				for (int i = 0; i < list.size(); i++) {
					ContentValues values = new ContentValues();
					values.put(BrandSQLiteHelper.ORDERNUM, list.get(i)
							.getOrderNum());
					values.put(BrandSQLiteHelper.BRANDID, list.get(i)
							.getBrandID());
					values.put(BrandSQLiteHelper.BRANDNAME, list.get(i)
							.getBrandName());
					values.put(BrandSQLiteHelper.LOGO, list.get(i).getLogo());
					values.put(BrandSQLiteHelper.ATOZ, list.get(i).getAtoZ());
					values.put(BrandSQLiteHelper.BRANDSIMPLENAME, list.get(i)
							.getBrandSimpleName());
					db.insert(TABNAME, null, values);
				}
			}
			c.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public List<BrandDetail> getAtoZList() {
		// db = dbHelper.getReadableDatabase();
		int j = 0, k = 0;
		List<BrandDetail> list = new ArrayList<BrandDetail>();
		if (db == null) {
			return list;
		}
		try {

			Cursor cursor = db.query(TABNAME, null, null, null, null, null,
					"AtoZ");
			while (cursor.moveToNext()) {
				BrandDetail brandDetail = new BrandDetail();
				brandDetail.setBrandID(Integer.valueOf(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.BRANDID))));
				brandDetail.setBrandName(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.BRANDNAME)));
				brandDetail.setLogo(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.LOGO)));
				brandDetail.setOrderNum(Integer.valueOf(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.ORDERNUM))));
				brandDetail.setAtoZ(cursor.getString(cursor
						.getColumnIndex("AtoZ")));
				brandDetail.setBrandSimpleName(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.BRANDSIMPLENAME)));
				list.add(brandDetail);

			}
			cursor.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return list;
	}

	/**
	 * 根据tag查询数据库
	 * @param tag
	 * @return
	 */
	public List<BrandDetail> getAtoZListByTag(String tag) {

		List<BrandDetail> list = new ArrayList<BrandDetail>();
		if (db == null) {
			return list;
		}
		try {

			Cursor cursor = db.query(TABNAME, null, "BrandType=?", new String[]{tag}, null, null,
					"LOWER(AtoZ) asc");
			while (cursor.moveToNext()) {
				BrandDetail brandDetail = new BrandDetail();
				brandDetail.setBrandID(Integer.valueOf(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.BRANDID))));
				brandDetail.setBrandName(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.BRANDNAME)));
				brandDetail.setLogo(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.LOGO)));
				brandDetail.setOrderNum(Integer.valueOf(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.ORDERNUM))));
				brandDetail.setAtoZ(cursor.getString(cursor
						.getColumnIndex("AtoZ")));
				brandDetail.setBrandSimpleName(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.BRANDSIMPLENAME)));
				list.add(brandDetail);

			}
			cursor.close();
		} catch (Exception e) {
			
		}
		return list;
	}

	public List<BrandDetail> getBrandList() {
		List<BrandDetail> list = new ArrayList<BrandDetail>();
		if (db == null) {
			return list;
		}
		Cursor cursor = null;
		try {

			cursor = db.query(TABNAME, null, null, null, null, null,
					"OrderNum asc,BrandID,asc");
			while (cursor.moveToNext()) {

				BrandDetail brandDetail = new BrandDetail();
				brandDetail.setBrandID(Integer.valueOf(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.BRANDID))));
				brandDetail.setBrandName(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.BRANDNAME)));
				brandDetail.setLogo(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.LOGO)));
				brandDetail.setOrderNum(Integer.valueOf(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.ORDERNUM))));
				brandDetail.setAtoZ(cursor.getString(cursor
						.getColumnIndex("AtoZ")));
				brandDetail.setBrandSimpleName(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.BRANDSIMPLENAME)));
				brandDetail.setBrandType(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.BRANDTYPE)));
				list.add(brandDetail);

			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			cursor.close();
		}
		return list;
	}

	public List<BrandDetail> getBrandListByBrandType(String type)
			throws Exception {
		List<BrandDetail> list = new ArrayList<BrandDetail>();
		if (db == null) {
			return list;
		}
		try {
			Cursor cursor = db.query(TABNAME, null, "BrandType=?",
					new String[] { type }, null, null, "OrderNum asc,BrandID asc");
			while (cursor.moveToNext()) {
				BrandDetail brandDetail = new BrandDetail();
				brandDetail.setBrandID(Integer.valueOf(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.BRANDID))));
				brandDetail.setBrandName(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.BRANDNAME)));
				brandDetail.setLogo(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.LOGO)));
				brandDetail.setOrderNum(Integer.valueOf(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.ORDERNUM))));
				brandDetail.setAtoZ(cursor.getString(cursor
						.getColumnIndex("AtoZ")));
				brandDetail.setIsDeleted(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.ISDELETED)));
				brandDetail.setBrandSimpleName(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.BRANDSIMPLENAME)));
				brandDetail.setBrandType(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.BRANDTYPE)));
				list.add(brandDetail);

			}

			cursor.close();
		} catch (Exception e) {
			throw e;
		}
		return list;
	}

	public void upDateList(List<BrandDetail> upDateList) throws Exception {

		if (upDateList == null || upDateList.size() == 0 || db == null) {
			return;
		}
		try {

			for (int i = 0; i < upDateList.size(); i++) {
				boolean insert = false;
				ContentValues values = null;
				if (upDateList.get(i).getIsDeleted().equals("True")) {
					db.delete(TABNAME, "BrandID=?", new String[] { String.valueOf(upDateList
							.get(i).getBrandID()) });
				} else {
					// Log.i(TAG, "upDateList-->" +
					// upDateList.get(i).getBrandID());
					String brandName = upDateList.get(i).getBrandName();
					String brandLogo = upDateList.get(i).getLogo();
					int brandOrderNum = upDateList.get(i).getOrderNum();
					String brandAtoZ = upDateList.get(i).getAtoZ();
					String brandSimpleName = upDateList.get(i)
							.getBrandSimpleName();
					int brandID = upDateList.get(i).getBrandID();
					values = new ContentValues();
					values.put(BrandSQLiteHelper.BRANDID, brandID);
					values.put(BrandSQLiteHelper.BRANDNAME, brandName);
					values.put(BrandSQLiteHelper.LOGO, brandLogo);
					values.put(BrandSQLiteHelper.ORDERNUM, brandOrderNum);
					values.put(BrandSQLiteHelper.ATOZ, brandAtoZ);
					values.put(BrandSQLiteHelper.BRANDSIMPLENAME,
							brandSimpleName);
					int c = db.update(TABNAME, values, "BrandID=?",
							new String[] { String.valueOf(upDateList.get(i).getBrandID()) });
					if (c <= 0) {
						insert = true;
					}
				}
				if (insert) {
					db.insert(TABNAME, null, values);
				}

			}
		} catch (Exception e) {
			Log.i("hz", e.toString());
			throw e;
		}
	}

	// 根据brandID查询
	public BrandDetail getBrandDetailwithBrandID(String brandID) {
		if (brandID == null) {
			return null;
		}
		BrandDetail brandDetail = new BrandDetail();
		try {

			Cursor cursor = db.query(TABNAME, null, "BrandID=?",
					new String[] { brandID }, null, null, null);
			while (cursor.moveToNext()) {
				brandDetail.setBrandID(Integer.valueOf(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.BRANDID))));
				brandDetail.setBrandName(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.BRANDNAME)));
				brandDetail.setLogo(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.LOGO)));
				brandDetail.setOrderNum(Integer.valueOf(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.ORDERNUM))));
				brandDetail.setAtoZ(cursor.getString(cursor
						.getColumnIndex("AtoZ")));
				brandDetail.setBrandSimpleName(cursor.getString(cursor
						.getColumnIndex(BrandSQLiteHelper.BRANDSIMPLENAME)));
			}
			cursor.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return brandDetail;
	}

}
